package g11.testdao;

import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertNull;
import g11.dao.DAOException;
import g11.dao.EmployeeDAO;
import g11.dao.EmployeeDAOImpl;
import g11.model.Employee;
import java.util.List;

import org.junit.Test;

public class testEmployeeDao {
	// test create, retrieve, update and delete
	@Test
	public void testEmployeeCRUD() throws DAOException {
		// initiate object
		Employee employee = new Employee();
		employee.setUsername("penny");
		employee.setPassword("1234");
		Byte type = 1;
		employee.setType(type);
		employee.setName("Penny Money");
		employee.setDepartment("Surgery");
		employee.setTitle("Surgery Doctor");
		// create start
		EmployeeDAO employeeDao = new EmployeeDAOImpl();
		employeeDao.createEmployee(employee);
		Employee retrievedEmployee = new Employee();
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");
		assertNotNull(retrievedEmployee);
		assertEquals("Surgery Doctor", retrievedEmployee.getTitle());
		// create end
		// update start
		retrievedEmployee.setName("Penny Yen");
		retrievedEmployee.setTitle("Inpatient Doctor");
		employeeDao.updateEmployee(retrievedEmployee);
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");
		assertNotNull(retrievedEmployee);
		assertEquals("Inpatient Doctor", retrievedEmployee.getTitle());
		assertEquals("Penny Yen", retrievedEmployee.getName());
		// update end
		// delete start
		
		/* test getAllDoctor()
		 * create another doctor first 
		 */
		employee.setUsername("Johnny");
		employee.setPassword("1234");
		type = 1;
		employee.setType(type);
		employee.setName("Johnny Money");
		employee.setDepartment("Clinic");
		employee.setTitle("Family Doctor");
		employeeDao.createEmployee(employee);
		
		List<Employee> empList=employeeDao.getAllEmployee("doctor");
		assertEquals("penny", empList.get(0).getUsername());
		assertEquals("Johnny", empList.get(1).getUsername());
		
		
		// delete start
		employeeDao.deleteEmployee(retrievedEmployee);
		employeeDao.deleteEmployee(employee);
		
		retrievedEmployee = employeeDao.getEmployeeByUsername("penny");
		assertNull(retrievedEmployee);
		// delete end
	}

}
